package com.wude.guild.holder;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mc.developmentkit.utils.MCUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.qamaster.android.util.Protocol;
import com.wude.bean.JifenDetailBean;
import com.wude.bean.TaskBean;
import com.wude.guild.R;
import com.wude.http.HttpCom;
import com.wude.http.HttpUtils;
import com.wude.tools.Utils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 唐小妹 on 2017/12/11 0011.
 */

public class EverydayTaskHolder extends BaseHolder<TaskBean> implements View.OnClickListener {
    @BindView(R.id.iv_icon)
    ImageView iv_icon;
    @BindView(R.id.tv_taskName)
    TextView tv_taskName; //每日领取流量
    @BindView(R.id.tv_points)
    TextView tv_points;//(+1/次)
    @BindView(R.id.tv_desc)
    TextView tv_desc;//文案暂定
    @BindView(R.id.tv_complete)
    TextView tv_complete;//(0/1)
    @BindView(R.id.tv_get)
    TextView tv_get;//领取

    private int white;
    private int black;

    @Override
    protected void refreshView(TaskBean taskBean, int position, Activity activity) {
        white = activity.getResources().getColor(R.color.white);
        black = activity.getResources().getColor(R.color.text_5050);

        MCUtils.fillImage(iv_icon, taskBean.icon);
        tv_taskName.setText(taskBean.title + "");
        tv_points.setText("(+" + taskBean.points + "/次)");
        tv_desc.setText(taskBean.desc + "");
        if (0 == taskBean.is_button) {
            tv_complete.setText("(" + taskBean.complete + "/" + taskBean.numb + ")");
            tv_complete.setVisibility(View.VISIBLE);
            tv_get.setVisibility(View.GONE);
        } else {
            tv_complete.setVisibility(View.GONE);
            tv_get.setVisibility(View.VISIBLE);
            if ("1".equals(taskBean.complete)) {//已经领取过
                setButton(false);
            } else {
                setButton(true);
            }

        }
    }

    private void setButton(boolean canClick) {
        if (canClick) {
            tv_get.setBackgroundResource(R.drawable.orange_corners_bg_5);
            tv_get.setTextColor(white);
            tv_get.setClickable(true);
            tv_get.setOnClickListener(this);
        } else {
            tv_get.setBackgroundResource(R.color.transparency);
            tv_get.setTextColor(black);
            tv_get.setClickable(false);
            tv_get.setText("已领取");
        }
    }

    @Override
    protected View initView() {
        View inflate = LayoutInflater.from(x.app()).inflate(R.layout.everyday_task_holder, null);
        ButterKnife.bind(this, inflate);
        inflate.setTag(this);
        return inflate;
    }

    @Override
    public void onClick(View view) {
        Map<String, String> map = new HashMap<>();
        map.put("token", Utils.getLoginUser().token);
        HttpCom.POST(handler, HttpCom.getGetUserDailyMission(), map, false);
        tv_get.setClickable(false);
    }

    /**
     * 获取完成任务积分
     */
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        int status = jsonObject.getInt("status");
                        String return_code = jsonObject.getString("return_code");
                        ToastUtil.showToast(return_code);
                        if (status == 1) {
                            setButton(false);
                        } else {
                            setButton(true);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    tv_get.setClickable(true);
                    ToastUtil.showToast("网络错误");
                    break;
            }
        }
    };
}
